#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

struct Catalog {
    long phone;
    string name;
    string surname;
    string club;
};

bool lesssA(struct Catalog x, struct Catalog y) {
    if (x.name != y.name) {
        return x.name < y.name;
    }
    return x.surname < y.surname;
}

bool lesssB(struct Catalog x, struct Catalog y) {
    if (x.club != y.club) {
        return x.club < y.club;
    }
    return x.surname < y.surname;
}

void SelectSortA(struct Catalog contacts[], int n, int mas[]) {
    int k = 0, temp;
    for (int i = 0; i < n - 1; i++) {
        k = i;
        for (int j = i + 1; j < n; j++) {
            if (lesssA(contacts[j], contacts[k])) {
                k = j;
            }
        }
        if (k != i) {
            temp = mas[i];
            mas[i] = mas[k];
            mas[k] = temp;
        }
    }
}

void SelectSortB(struct Catalog contacts[], int n, int mas[]) {
    int k = 0, temp;
    for (int i = 0; i < n - 1; i++) {
        k = i;
        for (int j = i + 1; j < n; j++) {
            if (lesssB(contacts[mas[j]], contacts[mas[k]])) {
                k = j;
            }
        }
        if (k != i) {
            temp = mas[i];
            mas[i] = mas[k];
            mas[k] = temp;
        }
    }
}

void PrintMas(Catalog a[], int n) {
    for (int i = 0; i < n; i++) {
        cout << a[i].name << " " << a[i].surname << " " << a[i].phone << " " << a[i].club << endl;
    }
    cout << endl;
}

int main(){
    int n = 5;
    Catalog catalog[] = {
        {89619207846, "Александр", "Аксёненко", "Металлург"},
        {89699222795, "Николай", "Голдобин", "Спартак"},
        {89159084663, "Денис", "Голубев", "Адмирал"},
        {89192736469, "Никита", "Коротков", "Сибирь"},
        {89299268512, "Александр", "Шаров", "Салават"}
    };
    
    int A[n], B[n], C[n];
    
    PrintMas(catalog, n);
    for(int i = 0; i < n; i++){
        A[i] = i;
        B[i] = i;
        C[i] = i;
    }
    
    SelectSortA(catalog, n, A);
    SelectSortB(catalog, n, B);
    
    cout << "Отсортированный по имени и фамилии:\n\n";
    for(int i = 0; i < n; i++){
        cout << catalog[A[i]].name << " " << catalog[A[i]].surname << " " << catalog[A[i]].phone << " " << catalog[A[i]].club << endl;
    }
    
    cout << "\nОтсортированный по клубу и фамилии:\n\n";
    for(int i = 0; i < n; i++){
        cout << catalog[B[i]].name << " " << catalog[B[i]].surname << " " << catalog[B[i]].phone << " " << catalog[B[i]].club << endl;
    }
    
    cout << endl << "Исходные индексы: ";
    for(int i = 0; i < n; i++){
        cout << C[i] << " ";
    }
    cout << endl;
    cout << "Для первой сортировки: ";
    for(int i = 0; i < n; i++){
        cout << A[i] << " ";
    }
    cout << endl;
    cout << "Для второй сортировки: ";
    for(int i = 0; i < n; i++){
        cout << B[i] << " ";
    }
    cout << endl;
    
    return 0;
}


